﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
using System.Diagnostics;

using LowLevelGraphics;
using LowLevelGraphics.Filter;

namespace ImageRecognition2.Forms
{
    /// <summary>
    /// FormTypeCommand
    /// </summary>
    public partial class FormTypeCommand : Form
    {
        private List<string> m_StringList = new List<string>();

        /// <summary>
        /// Initializes a new instance of the <see cref="FormTypeCommand"/> class.
        /// </summary>
        public FormTypeCommand()
        {
            InitializeComponent();

            AutoCompleteStringCollection aAutoCompleteFilterList = Definitions.GetAutoCompleteFromFilterList();
            textBox1.AutoCompleteCustomSource = aAutoCompleteFilterList;
            textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;

            StreamReader reader = new StreamReader(ImageRecognition2Definitions.FilterTypeStringListFile);
            string[] aString = Regex.Split(reader.ReadToEnd(), Environment.NewLine);

            m_StringList.AddRange(aString);
        }

        /// <summary>
        /// Handles the Click event of the cbOK control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void cbOK_Click(object sender, EventArgs e)
        {
            ExecuteFilter(textBox1.Text);
            this.DialogResult = DialogResult.OK;

            this.Close();
        }

        /// <summary>
        /// Executes the filter.
        /// </summary>
        private void ExecuteFilter(string _sText)
        {
            string sTextBoxText = _sText;

            List<string> aString = m_StringList.FindAll(delegate(string _sString) { return _sString.ToLower().Contains(sTextBoxText.ToLower()); });
            string sString = string.Empty;
            foreach (string sTempString in aString)
            {
                if (sTempString.EndsWith("."+sTextBoxText))
                {
                    sString = sTempString;
                    break;
                }
            }

            if (sString.Length == 0)
            {
                Debug.WriteLine(string.Format("Filter {0} not found", sTextBoxText));
                return;
            }

            if (MainFormSingleton.ActivePictureBox == null) return;

            Bitmap bitmap = (Bitmap)MainFormSingleton.ActivePictureBox.BackgroundImage;
            IFilter filter = (IFilter)Activator.CreateInstance(Definitions.Assembly.GetName().FullName, sString).Unwrap();//GetFilter(sLine);
            if (filter != null)
            {
                filter.Execute(bitmap).Dispose();
                MainFormSingleton.LastExecutedFilter = filter;
            }
            filter = null;
            MainFormSingleton.ActivePictureBox.Invalidate();
        }

        /// <summary>
        /// Handles the Click event of the cbCancel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void cbCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        /// <summary>
        /// Input text
        /// </summary>
        public string InputText
        {
            get { return textBox1.Text; }
        }
    }
}
